package com.far.dao;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import oracle.jdbc.OracleTypes;
import utiles.AppProperties;
import utiles.ConexionPool;

import com.portal.dao.PacienteFarmaciaDAO;
import com.portal.dto.PacienteDTO;

public class FarMedicoDAO {
	
	private static Logger log = Logger.getLogger(PacienteFarmaciaDAO.class);
	String pkgVar = (AppProperties.getProperty("aplicacion", "PACKAGE"));
	
	public  List<PacienteDTO> getMedicosByRut(String rut) throws SQLException, IOException{
		//ConexionPool connDao = new ConexionPool();
		Connection conn = ConexionPool.getConnectionPool();	
    	List<PacienteDTO> listMedico = new ArrayList<PacienteDTO>();
		try {
			String query = "{ call "+pkgVar+".Pkg_pac_paciente.getMedicosByRut(?,?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, rut);
			stmt.registerOutParameter(2, OracleTypes.CURSOR);
			stmt.execute();
			ResultSet rs = (ResultSet)stmt.getObject(2);			
			while (rs.next()) {
				PacienteDTO listmed = new PacienteDTO();
				listmed.setRut(rs.getString(1));
				listmed.setDv(rs.getString(2));
				listmed.setNombreCompleto(rs.getString(3));
				listMedico.add(listmed);
			}			
			rs.close();
			stmt.close();
		} catch (SQLException ex) {  
			log.error("ErrorfarMedicoDAO.getMedicosByRut SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			//ConexionPool.desconectaPool(conn);
			conn.close();			
        
            conn = null;
		}
		return listMedico;	
	}

}
